package com.echo.yixiaowu.feature.components

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.echo.yixiaowu.R
import com.echo.yixiaowu.data.model.player.CommentData

@Composable
fun UserAvatarComponent(
    modifier: Modifier = Modifier,
    userName: String = "996大王",
    detailData: String = "11 视频  10w粉丝",
    actions: @Composable () -> Unit = {}
) {
    Row(
        modifier = modifier,
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.SpaceBetween,
    ) {
        Image(
            modifier = Modifier
                .size(40.dp)
                .clip(CircleShape)
                .background(Color.Gray),
            painter = painterResource(R.drawable.image1),
            contentDescription = "User Avatar"
        )
        Column(
            modifier = Modifier
                .weight(1f)
                .padding(start = 12.dp),
            horizontalAlignment = Alignment.Start,
            verticalArrangement = Arrangement.spacedBy(1.dp)
        ) {
            Text(
                text = userName, fontSize = 14.sp, color = Color.Black, textAlign = TextAlign.Start
            )
            Text(
                text = detailData,
                fontSize = 12.sp,
                color = Color.Gray,
                textAlign = TextAlign.Start
            )
        }
        actions()
    }
}

/**
 * 评论区compose
 */
@Composable
fun UserAvatarComment(
    modifier: Modifier = Modifier,
    commentData: CommentData,
    likeColor: Color = Color.Gray,
    onLikeClick: () -> Unit = {},
    dislikeColor: Color = Color.Gray,
    onDislikeClick: () -> Unit = {},
    favoriteColor: Color = Color.Gray,
    onFavoriteClick: () -> Unit = {},
    shareColor: Color = Color.Gray,
    onShareClick: () -> Unit = {},
) {
    Column(modifier = modifier) {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            Image(
                modifier = Modifier
                    .size(40.dp)
                    .clip(CircleShape)
                    .background(Color.Gray),
                painter = painterResource(commentData.avatarResId),
                contentDescription = "User Avatar"
            )
            Column(
                modifier = Modifier
                    .weight(1f)
                    .padding(start = 12.dp),
                horizontalAlignment = Alignment.Start,
                verticalArrangement = Arrangement.spacedBy(1.dp)
            ) {
                Text(
                    text = commentData.authorName,
                    fontSize = 16.sp,
                    color = Color.Black,
                    textAlign = TextAlign.Start
                )
                Text(
                    text = "50分钟前 IP属地：马达加斯加",
                    fontSize = 12.sp,
                    color = Color.Gray,
                    textAlign = TextAlign.Start
                )
            }

        }
        Column(modifier = Modifier.padding(start = 52.dp, top = 6.dp)) {
            Text(
                text = commentData.content,
                style = MaterialTheme.typography.bodyMedium,
                color = Color.Gray,
                textAlign = TextAlign.Start
            )

            Row(
                modifier = Modifier.padding(top = 8.dp),
                horizontalArrangement = Arrangement.spacedBy(24.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                ActionIcon(
                    modifier = Modifier
                        .padding(start = 2.dp)
                        .size(16.dp),
                    imageVector = ImageVector.vectorResource(R.drawable.ic_thumb_up),
                    contentDescription = "点赞",
                    tint = likeColor,
                    onClick = onLikeClick
                )

                ActionIcon(
                    modifier = Modifier
                        .padding(start = 2.dp)
                        .size(16.dp),
                    imageVector = ImageVector.vectorResource(R.drawable.ic_not_like),
                    contentDescription = "踩",
                    tint = dislikeColor,
                    onClick = onDislikeClick
                )

                ActionIcon(
                    modifier = Modifier
                        .padding(start = 2.dp)
                        .size(16.dp),
                    imageVector = ImageVector.vectorResource(R.drawable.ic_star),
                    contentDescription = "收藏",
                    tint = favoriteColor,
                    onClick = onFavoriteClick
                )

                ActionIcon(
                    modifier = Modifier
                        .padding(start = 2.dp)
                        .size(16.dp),
                    imageVector = ImageVector.vectorResource(R.drawable.ic_comment),
                    contentDescription = "分享",
                    tint = shareColor,
                    onClick = onShareClick
                )
            }
        }

    }
}

@Composable
fun ActionIcon(
    modifier: Modifier = Modifier,
    imageVector: ImageVector,
    contentDescription: String,
    tint: Color,
    onClick: () -> Unit
) {
    val interactionSource = remember { MutableInteractionSource() }

    Icon(
        imageVector = imageVector,
        contentDescription = contentDescription,
        tint = tint,
        modifier = modifier.clickable(
            interactionSource = interactionSource, indication = null,
            onClick = onClick
        )
    )
}